package com.SpringCloud.code;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;
import java.util.Scanner;

public class FullCodeGenerator {

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        final String projectPath = System.getProperty("user.dir") + "/generator-code";
        final String outputDir = projectPath + "/src/main/java";
        final String mapperXmlDir = projectPath + "/src/main/resources/mapper";

        FastAutoGenerator.create("jdbc:postgresql://192.168.244.118:5432/azk",
                        "postgres", "postgres")
                .globalConfig(builder -> {
                    builder
                            .outputDir(outputDir)
                            .disableOpenDir(); // 不自动打开目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.SpringCloud.code")
                            .entity("entity")
                            .mapper("mapper")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, mapperXmlDir));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(scanner("表名，多个英文逗号分割").split(","))
                            .addTablePrefix("t_", "c_");

                    // Entity 配置
                    builder.entityBuilder()
                            .enableLombok()
                            .naming(NamingStrategy.underline_to_camel)
                            .columnNaming(NamingStrategy.underline_to_camel)
                            .enableTableFieldAnnotation()
                            .disableSerialVersionUID()
                            .formatFileName("%s")
                            .enableFileOverride(); // 覆盖实体类

                    // Mapper 配置
                    builder.mapperBuilder()
                            .enableMapperAnnotation()
                            .enableBaseResultMap() // 生成基本的resultMap
                            .enableBaseColumnList() // 生成基本的SQL片段
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            .enableFileOverride(); // 覆盖Mapper文件

                    // Service 配置
                    builder.serviceBuilder();
                           // .enableFileOverride(); // 覆盖 Service 接口和实现

                    // Controller 配置
                    builder.controllerBuilder()
                            .enableRestStyle();
                            //.enableFileOverride(); // 覆盖 Controller
                })
                // 模板配置
                .templateConfig(builder -> {
                    builder.entity("templates/entity.java"); // 使用自定义模板路径
                })
                // 使用 Freemarker 模板引擎
                .templateEngine(new FreemarkerTemplateEngine())
                // 执行生成
                .execute();
    }
}
